//
//  SettingsViewController.m
//  厚积薄发
//
//  Created by 侯振兴 on 14/12/31.
//  Copyright (c) 2014年 侯振兴. All rights reserved.
//

#import "SettingsViewController.h"
//#import "HomePageViewController.h"
//#import "SettingsTableViewCell.h"

#import "WenZiViewController.h"
#import "OpinionViewController.h"
#import "OurViewController.h"
#import "OffLineViewController.h"
#import "DengLuViewController.h"
#import <ShareSDK/ShareSDK.h>


@interface SettingsViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation SettingsViewController


- (void)dealloc
{
    [super dealloc];
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title = @"个人设置";
    
//    SettingsViewController *setting = [[[SettingsViewController alloc] init] autorelease];
//    UINavigationController *settingNav = [[[UINavigationController alloc] initWithRootViewController:setting] autorelease];
//    [settingNav.navigationBar setBackgroundImage:[UIImage imageNamed:@"0.png"] forBarMetrics:UIBarMetricsDefault];
//    UILabel *newsLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 200, 44)] autorelease];
//    newsLabel.text = @"设置";
//    newsLabel.font = [UIFont systemFontOfSize:30];
//    newsLabel.textAlignment = NSTextAlignmentCenter;
//    newsLabel.textColor = [UIColor blueColor];
//    setting.navigationItem.titleView = newsLabel;
//    [self addChildViewController:settingNav];
//    [self.view addSubview:settingNav.view];
//    settingNav.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64);
    // Do any additional setup after loading the view.
    [self drawTableView];
}
-(void)drawTableView{
    UITableView *tview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 120) style:UITableViewStyleGrouped];
    [tview setDelegate:self];
    [tview setDataSource:self];
    [self.view addSubview:tview];
    [tview release];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ( section == 3 || section == 1) {
        return 3;
    } else if (section == 2) {
        return 2;
    } else {
        return 1;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    switch (indexPath.section) {
        case 0:
            cell.textLabel.text = @"点击登陆";
            cell.imageView.image = [UIImage imageNamed:@"geren.png"];
            break;
        case 1:
            if (indexPath.row == 0) {
                cell.textLabel.text =  @"仅wifi下允许下图";
                cell.imageView.image = [UIImage imageNamed:@"wifi.png"];
            } else if (indexPath.row == 1) {
                cell.textLabel.text = @"文章字号";
                cell.imageView.image = [UIImage imageNamed:@"wenzihao.png"];
            } else {
                cell.textLabel.text = @"离线设置";
                cell.imageView.image = [UIImage imageNamed:@"lixian.png"];
            }
            break;
        case 2:
            if (indexPath.row == 0) {
                cell.textLabel.text =  @"清空缓存";
                cell.imageView.image = [UIImage imageNamed:@"qingkong.png"];
            } else {
                cell.textLabel.text = @"清空'我的馆藏'";
                cell.imageView.image = [UIImage imageNamed:@"qingkong2.png"];
            }
            break;
        case 3:
            if(indexPath.row == 0)
            {
                cell.textLabel.text =  @"检查更新";
                cell.imageView.image = [UIImage imageNamed:@"gengxin.png"];
            } else if (indexPath.row == 1) {
                cell.textLabel.text = @"意见反馈";
                cell.imageView.image = [UIImage imageNamed:@"fankui.png"];
            } else {
                cell.textLabel.text = @"关于我们";
                cell.imageView.image = [UIImage imageNamed:@"guanyuwomen.png"];
            }
            break;
        case 4:
            cell.textLabel.text = @"退出登陆";
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
            break;
        default:
            break;
    }
#pragma mark -
#pragma mark TableViewGroup下面的Button创建
    
    //    CGRect bounds = [[cell contentView] bounds];
    //    UIButton *btLogOut = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [btLogOut setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
    //    [btLogOut setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    
    //    [btLogOut setBackgroundImage:[UIImage imageNamed:@"NU_Logout_up.png"] forState:UIControlStateNormal];
    //    [btLogOut setBackgroundImage:[UIImage imageNamed:@"NU_Logout_down.png"] forState:UIControlStateHighlighted];
    
    //    [btLogOut setTitle:@"退出登陆" forState:UIControlStateNormal];
    //    [btLogOut setFrame:bounds];
    //    [btLogOut addTarget:self action:@selector(btLogOutTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    //
    //    [[cell contentView] addSubview:btLogOut];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        DengLuViewController *dengController = [[DengLuViewController alloc] init];
        [self.navigationController pushViewController:dengController animated:YES];
        [dengController release];
    } else if (indexPath.section == 1 && indexPath.row == 1) {
        WenZiViewController *wenZiController = [[WenZiViewController alloc] init];
        [self.navigationController pushViewController:wenZiController animated:YES];
        [wenZiController release];
    } else if (indexPath.section == 1 && indexPath.row == 2) {
        OffLineViewController *offController = [[OffLineViewController alloc] init];
        [self.navigationController pushViewController:offController animated:YES];
        [offController release];
    } else if (indexPath.section == 2 && indexPath.row == 0) {
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"确定清除缓存吗?" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [alert1 show];
        [alert1 release];
    } else if (indexPath.section == 2 && indexPath.row == 1) {
        UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"要清空收藏记录吗?" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [alert2 show];
        [alert2 release];
    }
    else if (indexPath.section == 3 && indexPath.row == 0) {
        self.alert = [[UIAlertView alloc] initWithTitle:@"已经是最新版本了哦!" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [_alert show];
        
        [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(timerAction:) userInfo:nil repeats:NO];
        
        [_alert release];
        
    } else if (indexPath.section == 3 && indexPath.row == 1) {
        OpinionViewController *opinionController = [[OpinionViewController alloc] init];
        [self.navigationController pushViewController:opinionController animated:YES];
        [opinionController release];
    } else if (indexPath.section == 3 && indexPath.row == 2) {
        OurViewController *ourController = [[OurViewController alloc] init];
        [self.navigationController pushViewController:ourController animated:YES];
        [ourController release];
    } else if (indexPath.section == 4) {
        [ShareSDK cancelAuthWithType:ShareTypeSinaWeibo];
        [self.navigationController popViewControllerAnimated:NO];
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setObject:@"no" forKey:@"login"];
    }
    
}

- (void)timerAction:(NSTimer *)timer
{
    [_alert dismissWithClickedButtonIndex:0 animated:YES];
}

- (void)btLogOutTouchUpInside:(id)sender
{
    
}


//- (void)dealloc
//{
//    [_tableArray release];
//    [super dealloc];
//}
//
//- (instancetype)init
//{
//    self = [super init];
//    if (self) {
//        self.tableArray = [NSMutableArray array];
//        
//        NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
//        [dic1 setObject:@"点击登陆" forKey:@"name"];
//        [dic1 setObject:@"geren.png" forKey:@"imageName"];
//        
//        NSMutableDictionary *dic2 = [NSMutableDictionary dictionary];
//        [dic2 setObject:@"仅wifi下允许下图" forKey:@"name"];
//        [dic2 setObject:@"wifi.png" forKey:@"imageName"];
//        
//        NSMutableDictionary *dic3 = [NSMutableDictionary dictionary];
//        [dic3 setObject:@"文章字号" forKey:@"name"];
//        [dic3 setObject:@"wenzihao.png" forKey:@"imageName"];
//        
//        NSMutableDictionary *dic4 = [NSMutableDictionary dictionary];
//        [dic4 setObject:@"夜间模式" forKey:@"name"];
//        [dic4 setObject:@"yejian.png" forKey:@"imageName"];
//        
//        NSMutableDictionary *dic5 = [NSMutableDictionary dictionary];
//        [dic5 setObject:@"离线设置" forKey:@"name"];
//        [dic5 setObject:@"lixian.png" forKey:@"imageName"];
//        
//        NSMutableDictionary *dic6 = [NSMutableDictionary dictionary];
//        [dic6 setObject:@"清空缓存" forKey:@"name"];
//        [dic6 setObject:@"qingkong.png" forKey:@"imageName"];
//        
//        NSMutableDictionary *dic7 = [NSMutableDictionary dictionary];
//        [dic7 setObject:@"清空'阅览室'离线阅读" forKey:@"name"];
//        [dic7 setObject:@"qingkong1.png" forKey:@"imageName"];
//        
//        NSMutableDictionary *dic8 = [NSMutableDictionary dictionary];
//        [dic8 setObject:@"清空'我的馆藏'下载" forKey:@"name"];
//        [dic8 setObject:@"qingkong2.png" forKey:@"imageName"];
//        
//        NSMutableDictionary *dic9 = [NSMutableDictionary dictionary];
//        [dic9 setObject:@"检查更新" forKey:@"name"];
//        [dic9 setObject:@"gengxin.png" forKey:@"imageName"];
//        
//        NSMutableDictionary *dic10 = [NSMutableDictionary dictionary];
//        [dic10 setObject:@"意见反馈" forKey:@"name"];
//        [dic10 setObject:@"fankui.png" forKey:@"imageName"];
//        
//        NSMutableDictionary *dic11 = [NSMutableDictionary dictionary];
//        [dic11 setObject:@"关于我们" forKey:@"name"];
//        [dic11 setObject:@"guanyuwomen.png" forKey:@"imageName"];
//        
//        NSMutableDictionary *dic12 = [NSMutableDictionary dictionary];
//        [dic12 setObject:@"退出登陆" forKey:@"name"];
//        
//        NSMutableArray *array1 = [NSMutableArray array];
//        [array1 addObject:dic1];
//        
//        NSMutableArray *array2 = [NSMutableArray array];
//        [array2 addObject:dic2];
//        [array2 addObject:dic3];
//        [array2 addObject:dic4];
//        [array2 addObject:dic5];
//        
//        NSMutableArray *array3 = [NSMutableArray array];
//        [array3 addObject:dic6];
//        [array3 addObject:dic7];
//        [array3 addObject:dic8];
//        
//        NSMutableArray *array4 = [NSMutableArray array];
//        [array4 addObject:dic9];
//        [array4 addObject:dic10];
//        [array4 addObject:dic11];
//        
//        NSMutableArray *array5 = [NSMutableArray array];
//        [array5 addObject:dic12];
//        
//        NSMutableDictionary *bigDic1 = [NSMutableDictionary dictionary];
//        [bigDic1 setObject:array1 forKey:@"array"];
//        [bigDic1 setObject:@"     " forKey:@"title"];
//        
//        NSMutableDictionary *bigDic2 = [NSMutableDictionary dictionary];
//        [bigDic2 setObject:array2 forKey:@"array"];
//        [bigDic2 setObject:@"     " forKey:@"title"];
//        
//        NSMutableDictionary *bigDic3 = [NSMutableDictionary dictionary];
//        [bigDic3 setObject:array3 forKey:@"array"];
//        [bigDic3 setObject:@"     " forKey:@"title"];
//        
//        NSMutableDictionary *bigDic4 = [NSMutableDictionary dictionary];
//        [bigDic4 setObject:array4 forKey:@"array"];
//        [bigDic4 setObject:@"     " forKey:@"title"];
//        
//        NSMutableDictionary *bigDic5 = [NSMutableDictionary dictionary];
//        [bigDic5 setObject:array5 forKey:@"array"];
//        [bigDic5 setObject:@"     " forKey:@"title"];
//        
//        [_tableArray addObject:bigDic1];
//        [_tableArray addObject:bigDic2];
//        [_tableArray addObject:bigDic3];
//        [_tableArray addObject:bigDic4];
//        [_tableArray addObject:bigDic5];
//        
//    }
//    return self;
//}
//
//- (void)viewDidLoad {
//    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor whiteColor];
//    self.navigationItem.title = @"设置";
//    // Do any additional setup after loading the view.
//    [self createSetting];
//}
//
////-(void)MoreBtnClick
//
//- (void)createSetting;
//{
//    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49) style:UITableViewStylePlain];
//    _table.rowHeight = 60;
////    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height + 200)];
////    image.image = [UIImage imageNamed:@"wolfgang4"];
//    _table.dataSource = self;
//    _table.delegate = self;
//    [self.view addSubview:_table];
////    [_table addSubview:image];
////    [image release];
//
//}
//
//
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return [_tableArray count];
//}
//
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
//{
//    NSDictionary *dic = [_tableArray objectAtIndex:section];
//    NSArray *array = [dic objectForKey:@"array"];
//    return [array count];
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    static NSString *a = @"cell1";
//    SettingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
//    if (cell == nil) {
//        cell = [[[SettingsTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:a] autorelease];
//    }
//    
//    
//    NSDictionary *secDic = [_tableArray objectAtIndex:indexPath.section];
//    NSArray *array = [secDic objectForKey:@"array"];
//    NSDictionary *dic = [array objectAtIndex:indexPath.row];
//    NSString *name = [dic objectForKey:@"name"];
//    NSString *imageName = [dic objectForKey:@"imageName"];
//    
//    
//    if (indexPath.section == 4) {
//        cell.textLabel.text = name;
//        cell.textLabel.textAlignment = NSTextAlignmentCenter;
//        cell.backgroundColor = [UIColor redColor];
//    } else {
//        cell.textLabel.text = name;
//        cell.textLabel.textAlignment = NSTextAlignmentLeft;
//        
//    }
//    
//    UIImage *image = [UIImage imageNamed:imageName];
//    cell.imageView.image = image;
//    
//    if (indexPath.section == 0) {
//        _table.rowHeight = 100;
//    } else {
//        _table.rowHeight = 60;
//    }
//    
//    return cell;
//}
//
//
//
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    NSDictionary *dic = [_tableArray objectAtIndex:section];
//    NSString *title = [dic objectForKey:@"title"];
//    return title;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
